/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/
#include <stdio.h>
#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */

/* Definitions of physical drive number for each drive */
#define DEV_RAM		0	/* Example: Map Ramdisk to physical drive 0 */
#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
#define DEV_USB		2	/* Example: Map USB MSD to physical drive 2 */

#define DEV_DISK1	0		//disk1.vhd
#define DEV_DISK2	1		// disk2.vhd

#define SECTOR_SIZE		512

static const char* disk_path[2] = {
	"D:\\fatfs\\disk1.vhd",
	"D:\\fatfs\\disk2.vhd"
};

static FILE* disk_files[2] = { NULL, NULL };

/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	return disk_files[pdrv] ? RES_OK : STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	if ((pdrv != DEV_DISK1) && (pdrv != DEV_DISK2)) {
		printf("param error");
		return RES_PARERR;
	}

	disk_files[pdrv] = fopen(disk_path[pdrv], "rb+");
	if (disk_files[pdrv] == NULL) {
		printf("open disk file %s failed.\n", disk_path[pdrv]);
		return RES_ERROR;
	}
	return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	fseek(disk_files[pdrv], sector * SECTOR_SIZE, SEEK_SET);
	size_t size = SECTOR_SIZE * count;
	size_t bytes_read = fread(buff, 1, size, disk_files[pdrv]);
	if (bytes_read == size) {
		return RES_OK;
	}

	printf("read error");
	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	fseek(disk_files[pdrv], sector * SECTOR_SIZE, SEEK_SET);
	size_t size = SECTOR_SIZE * count;
	size_t bytes_written = fwrite(buff, 1, size, disk_files[pdrv]);
	if (bytes_written == size) {
		return RES_OK;
	}

	printf("write error");
	return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	// f_mkfs()
	switch (cmd) {
	case CTRL_SYNC:
		fflush(disk_files[pdrv]);
		return RES_OK;
	case GET_SECTOR_SIZE: {
		*(WORD*)buff = SECTOR_SIZE;
		return RES_OK;
	case GET_SECTOR_COUNT: {
		// disk1.vhd
		fseek(disk_files[pdrv], 0, SEC_E_OK);
		long file_size = ftell(disk_files[pdrv]);
		*(LBA_t*)buff = (file_size) / SECTOR_SIZE;
		return RES_OK;
	}
	}
	
	}
	return RES_PARERR;
}

#include <time.h>
DWORD get_fattime(void)
{
	return (DWORD)(2022 - 80) << 25 |
		(DWORD)(3 + 1) << 21 |
		(DWORD)1 << 16 |
		(DWORD)0 << 11 |
		(DWORD)0 << 5 |
		(DWORD)0 >> 1;
}

